// miniprogram/devTools/wanandroid/article/article.js
import {
  articleDetail
} from "../../../router"
Page({

  data: {
    scrollTop: 0,
    menuId: '',
    //文章列表数据
    articlePage: 1,
    articleList: [],
    //文章搜索数据
    hasMore: true,
    articlePageSearch: 1,
    articleListSearch: [],
    moreLoading: false,
    //输入信息
    searchInput: false,
    searchVal: '',
    hasMoreSearch: true
  },
  onLoad: function (options) {
    console.log(options)
    let menuId = options.menuId
    this.setData({
      menuId
    })
    this.getArticleHistory()
    if (options.menuName)
      wx.setNavigationBarTitle({
        title: options.menuName,
      })
  },
  onShareAppMessage: function () {

  },
  onReachBottom() {
    this.setData({
      moreLoading: true
    })
    let hasMore = this.data.hasMore
    let hasMoreSearch = this.data.hasMoreSearch
    let searchInput = this.data.searchInput
    hasMore = hasMore && !searchInput
    hasMoreSearch = hasMoreSearch && searchInput
    if (hasMore || hasMoreSearch) {
      this.getArticleHistory();
    } else {
      setTimeout(res => {
        this.setData({
          moreLoading: false
        })
        wx.showToast({
          title: '暂无更多数据',
          icon: 'none'
        })
      }, 1000)
    }
  },
  // 查询文章历史
  getArticleHistory() {
    // debugger
    let searchVal = this.data.searchVal
    let menuId = this.data.menuId
    let articlePage = this.data.articlePage
    let articlePageSearch = this.data.articlePageSearch
    let pageNum = searchVal ? articlePageSearch : articlePage
    if (pageNum == 1)
      wx.showLoading({
        title: '加载中...',
      })
    wx.cloud.callFunction({
      name: 'wanandroid',
      data: {
        action: "history",
        menuId: menuId,
        key: searchVal,
        pageNum,
      }
    }).then(res => {
      console.log(res)
      wx.hideLoading()
      let result = res.result.data
      let list = result.datas
      list.forEach(article => {
        article.title = this.replaceSpecialChar(article.title)
      })
      if (list.length == 0)
        wx.showToast({
          title: '暂无更多数据',
          icon: 'none'
        })
      if (searchVal) {
        this.setArticleListSearch(list, pageNum)
      } else {
        this.setArticleList(list, pageNum)
      }
    }).catch(err => {
      wx.hideLoading()
      console.error(err)
    })
  },
  //设置文章数据
  setArticleList(list, pageNum) {
    let hasMore = list.length > 0
    let mArticles = this.data.articleList
    let articleList = mArticles.concat(list)
    this.setData({
      articleList,
      moreLoading: false,
      articlePage: pageNum + 1,
      hasMore
    })
  },
  //设置文章搜索数据
  setArticleListSearch(list, pageNum) {
    let hasMoreSearch = list.length > 0
    let mArticles = this.data.articleListSearch
    let articleListSearch = mArticles.concat(list)
    this.setData({
      articleListSearch,
      moreLoading: false,
      hasMoreSearch,
      articlePageSearch: pageNum + 1
    })
  },
  //点击文章
  onArticleClick(e) {
    let title = e.currentTarget.dataset.title
    let link = e.currentTarget.dataset.link
    console.log(link)
    // wx.navigateTo({
    //   url: articleDetail + "?url=" + link + "&title=" + title
    // })
    wx.navigateToMiniProgram({
      appId: 'wx365c223d9be13e4e',
      path: `web?u=${link}`
    }).then(res => {
      console.log("onJumpToMini", res)
    }).catch(err => {
      console.error(err)
    })
  },
  confirmInputClick(e) {
    console.log("confirmInputClick", e)
    let searchVal = e.detail.value
    this.setData({
      searchVal
    })
    this.getArticleHistory()
  },
  onSearchClick(e) {
    this.setData({
      searchInput: true,
      hasMoreSearch: true,
      articleListSearch: []
    })
  },
  onCancelClick() {
    this.setData({
      searchVal: '',
      searchInput: false,
      articlePageSearch: 1
    })
  },
  onGoTopClick() {
    this.setData({
      scrollTop: 0
    })
  },
  replaceSpecialChar(url) {
    url = url.replace(/&rdquo;/g, '"');
    url = url.replace(/&ldquo;/g, '"');
    url = url.replace(/&quot;/g, '"');
    url = url.replace(/&amp;/g, '&');
    url = url.replace(/&lt;/g, '<');
    url = url.replace(/&gt;/g, '>');
    url = url.replace(/&nbsp;/g, ' ');
    // console.log("转义字符", url);
    return url;
  }
})